package com.m4f.city.business.security;

import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.m4f.city.business.domain.Application;
import com.m4f.city.business.service.ifc.IServiceLocator;

public class ApplicationDetailService implements UserDetailsService {
	
	private static final Logger LOGGER = Logger.getLogger(ApplicationDetailService.class.getName());
	
	@Autowired
	protected IServiceLocator serviceLocator;

	public ApplicationDetailService() {

	}
	
	public UserDetails loadUserByUsername(String clientId)
			throws UsernameNotFoundException, DataAccessException {
		try {
			Application app = this.serviceLocator.getApplicationService().getAppliaction(clientId);
			if(app != null) {
				return app;
			} else {
				throw new UsernameNotFoundException("Application not found: " + clientId);
			}
		} catch(Exception e) {
			throw new UsernameNotFoundException("Application not found: " + clientId);
		}
	}
}